{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n",
      "Iter0, Testing Accuracy: 0.8312\n",
      "Iter1, Testing Accuracy: 0.8708\n",
      "Iter2, Testing Accuracy: 0.8813\n",
      "Iter3, Testing Accuracy: 0.8883\n",
      "Iter4, Testing Accuracy: 0.8946\n",
      "Iter5, Testing Accuracy: 0.8981\n",
      "Iter6, Testing Accuracy: 0.8996\n",
      "Iter7, Testing Accuracy: 0.9011\n",
      "Iter8, Testing Accuracy: 0.9033\n",
      "Iter9, Testing Accuracy: 0.9053\n",
      "Iter10, Testing Accuracy: 0.9061\n",
      "Iter11, Testing Accuracy: 0.9075\n",
      "Iter12, Testing Accuracy: 0.9086\n",
      "Iter13, Testing Accuracy: 0.9092\n",
      "Iter14, Testing Accuracy: 0.9105\n",
      "Iter15, Testing Accuracy: 0.9109\n",
      "Iter16, Testing Accuracy: 0.9113\n",
      "Iter17, Testing Accuracy: 0.9119\n",
      "Iter18, Testing Accuracy: 0.9134\n",
      "Iter19, Testing Accuracy: 0.9128\n",
      "Iter20, Testing Accuracy: 0.9135\n"
     ]
    }
   ],
   "source": [
    "#load dataset\n",
    "mnist = input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "#define batch size\n",
    "batch_size = 100\n",
    "#calculate number of batches\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "#define placeholders\n",
    "x = tf.placeholder(tf.float32, [None,784])\n",
    "y = tf.placeholder(tf.float32, [None,10])\n",
    "\n",
    "#create simple NeuroNet\n",
    "W = tf.Variable(tf.zeros([784,10]))\n",
    "b = tf.Variable(tf.zeros([10]))\n",
    "prediction = tf.nn.softmax(tf.matmul(x,W)+b)\n",
    "\n",
    "#quadratic cost function\n",
    "loss = tf.reduce_mean(tf.square(y-prediction))\n",
    "# loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))\n",
    "#train with gradient descent\n",
    "train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "#initialize variables\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "#find accuracy of trained model\n",
    "correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))   #convert a list of booleans into a single boolean of accuracy\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for epoch in range(21):\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys = mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})\n",
    "        \n",
    "        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print(\"Iter\" + str(epoch) + \", Testing Accuracy: \" + str(acc))\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Trained with"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Trained with cross entropy:\n",
    "Iter0, Testing Accuracy: 0.8239\n",
    "Iter1, Testing Accuracy: 0.891\n",
    "Iter2, Testing Accuracy: 0.9007\n",
    "Iter3, Testing Accuracy: 0.9057\n",
    "Iter4, Testing Accuracy: 0.9079\n",
    "Iter5, Testing Accuracy: 0.9101\n",
    "Iter6, Testing Accuracy: 0.911\n",
    "Iter7, Testing Accuracy: 0.9132\n",
    "Iter8, Testing Accuracy: 0.9147\n",
    "Iter9, Testing Accuracy: 0.9159\n",
    "Iter10, Testing Accuracy: 0.9168\n",
    "Iter11, Testing Accuracy: 0.9185\n",
    "Iter12, Testing Accuracy: 0.918\n",
    "Iter13, Testing Accuracy: 0.9188\n",
    "Iter14, Testing Accuracy: 0.9191\n",
    "Iter15, Testing Accuracy: 0.9192\n",
    "Iter16, Testing Accuracy: 0.9218\n",
    "Iter17, Testing Accuracy: 0.9207\n",
    "Iter18, Testing Accuracy: 0.9218\n",
    "Iter19, Testing Accuracy: 0.9219\n",
    "Iter20, Testing Accuracy: 0.9211"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
